#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Export Item Detabase Toolkit

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_36"
__date__    = "2014/01/05"
__license__ = 'LGPL'

# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python
import json
from xml.dom.minidom import getDOMImplementation

# ESiPC

# Mikan
from mikan.idt import AbstractMikanItemDBToolkit
from mikan.core import MIKAN

# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------
class MIKAN_IDT_FROM(MIKAN):
	TYPE_TEXT = "text"
	TYPE_XML = "xml"
	TYPE_CSV = "csv"
	TYPE_JSON = "json"
#	TYPE_RSS = "rss"

class AbstractMikanFormIDT(AbstractMikanItemDBToolkit, MIKAN_IDT_FROM):
	def __init__(self, itemdb):
		super(AbstractMikanFormIDT, self).__init__(itemdb)

class MikanFormIDT(AbstractMikanFormIDT):
	def __init__(self, itemdb):
		super(MikanFormIDT, self).__init__(itemdb)


	def json_from_items(self, items, form):
		json = {}

		json["keys"] = []
		json["columns"] = {}
		json["items"] = []

		for field in form.sorted_fields():
			name = field.itemname
			if field.isvisibled and field.isresulttype:
				json["keys"].append(name)
				json["columns"][name] = field.itemcaption

		for item in items:
			v = {}
			for field in form.sorted_fields():
				if field.isresulttype:
					name = field.itemname
					v[name] = item[name]
			json["items"].append(v)

		return json

	def json_from_item(self, item, form):
		json = {}

		json["keys"] = []
		json["columns"] = {}
		json["items"] = []

		for field in form.sorted_fields():
			name = field.itemname
			if field.isvisibled:
				json["keys"].append(name)
				json["columns"][name] = field.itemcaption

		v = {}
		for field in form.sorted_fields():
			if field.isvisibled:
				name = field.itemname
				v[name] = item[name]
		json["items"].append(v)

		return json

	def json_from_create(self, item, form):
		json = {}

		json["items"] = []

		i = 0
		for field in form.sorted_fields():
			if field.isvisibled:
				v = {}
				v["name"] = "item_" + field.itemname
				v["caption"] = field.itemcaption
				v["type"] = field.jsontype
				if field.isrequired:
					v["required"] = field.isrequired
				if field.default:
					v["value"] = field.default
				else:
					v["value"] = u""
				v["edit"] = field.isenabled
				v["label"] = 3
				v["width"] = int(field.width /8)
				v["hint"] = ""
				v["sort"] = i
				json["items"].append(v)
				i += 1

		return json

# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------
if __name__ == '__main__':
	pass

# ------------------------------------------------------------
